package com.yungu.swift.assets.passenger.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.assets.passenger.dao.PassengerCouponMapper;
import com.yungu.swift.assets.passenger.model.dto.PassengerCouponDto;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerCouponPageParam;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerUuidPageParam;
import com.yungu.swift.assets.passenger.model.param.ApiPasPayCouponParam;
import com.yungu.swift.assets.passenger.model.vo.PassengerCouponExportVo;
import com.yungu.swift.assets.passenger.service.PassengerCouponService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.api.ApiBusinessPageParam;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.constants.PassengerConstant;
import com.yungu.swift.utils.DateUtils;
import com.yungu.swift.utils.MapUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;


/**
 * 乘客优惠券逻辑处理接口实现类
 *
 * @author cuixiuyin - Generate
 * @version 1.1.1-SNAPSHOT
 * @since 2018-07-03 18:03
 */
@Service
public class PassengerCouponServiceImpl extends BaseServiceImpl<PassengerCouponDto> implements PassengerCouponService {
    @Autowired
    private PassengerCouponMapper passengerCouponMapper;

    @Override
    protected IMapper<PassengerCouponDto> getMapper() {
        return passengerCouponMapper;
    }

    @Override
    public ResponseData<List<PassengerCouponDto>> getUseCouponList(ApiBusinessPageParam businessPageParam) {
        Map<String, Object> paramMap = MapUtils.build(4);
        paramMap.put("passengerUuid", businessPageParam.getUserUuid());
        paramMap.put("useBusinessType", businessPageParam.getBusinessType());
        paramMap.put("token", businessPageParam.getToken());
        PageBounds pageBounds = new PageBounds(businessPageParam.getNowPage(), businessPageParam.getPageSize());
        List<PassengerCouponDto> couponDtos = passengerCouponMapper.getCouponList(paramMap, pageBounds);
        if (couponDtos != null && couponDtos.size() > 0) {
            for (PassengerCouponDto passengerCouponDto : couponDtos) {
                StringBuilder builder = new StringBuilder("有效期：");
                builder.append(DateUtils.format(passengerCouponDto.getUseStartTime(), "yyyy-MM-dd"));
                builder.append(" 至 ");
                builder.append(DateUtils.format(passengerCouponDto.getUseEndTime(), "yyyy-MM-dd"));
                passengerCouponDto.setAliveTime(builder.toString());
                passengerCouponDto.setUseStartTime(null);
                passengerCouponDto.setUseEndTime(null);
            }
        }
        return ResponseData.buildSuccessResponse(couponDtos);
    }

    @Override
    public ResponseData<List<PassengerCouponDto>> getPayCouponList(ApiPasPayCouponParam couponParam) {
        Map<String, Object> paramMap = MapUtils.build(8);
        paramMap.put("passengerUuid", couponParam.getUserUuid());
        paramMap.put("useBusinessType", couponParam.getBusinessType());
        paramMap.put("totalFare", couponParam.getTotalFare());
        paramMap.put("cityUuid", couponParam.getOriginCityUuid());
        paramMap.put("uuid", couponParam.getCouponUuid());
        List<PassengerCouponDto> passengerCouponDtos = passengerCouponMapper.getPayCouponList(paramMap);
        if (CollectionUtils.isNotEmpty(passengerCouponDtos)) {
            for (PassengerCouponDto passengerCouponDto : passengerCouponDtos) {
                StringBuilder builder = new StringBuilder("有效期：");
                builder.append(DateUtils.format(passengerCouponDto.getUseStartTime(), "yyyy-MM-dd"));
                builder.append(" 至 ");
                builder.append(DateUtils.format(passengerCouponDto.getUseEndTime(), "yyyy-MM-dd"));
                passengerCouponDto.setAliveTime(builder.toString());
            }
            // 按金额大小排序
            Collections.sort(passengerCouponDtos, new Comparator<PassengerCouponDto>() {
                @Override
                public int compare(PassengerCouponDto a, PassengerCouponDto b) {
                    return (int) (b.getMoney() - a.getMoney());
                }
            });

        }
        return ResponseData.buildSuccessResponse(passengerCouponDtos);
    }

    @Override
    public ResponseData<PassengerCouponDto> findMinCoupon(String passengerUuid, Integer useBusinessType, Double tripTotalFare) {
        ApiPasPayCouponParam couponParam = new ApiPasPayCouponParam();
        couponParam.setUserUuid(passengerUuid);
        couponParam.setTotalFare(tripTotalFare);
        couponParam.setBusinessType(useBusinessType);
        return findMinCoupon(couponParam);
    }

    @Override
    public ResponseData<PassengerCouponDto> findMinCoupon(ApiPasPayCouponParam couponParam) {
        ResponseData<List<PassengerCouponDto>> responseData = getPayCouponList(couponParam);
        if (responseData.isSuccess() && CollectionUtils.isNotEmpty(responseData.getData())) {
            return ResponseData.buildSuccessResponse(responseData.getData().get(0));
        }
        return ResponseData.buildSuccessResponse(null);
    }

    @Override
    public ResponseData<Integer> getCount(String passengerUuid) {
        Map params = MapUtils.build(2);
        params.put("passengerUuid", passengerUuid);
        params.put("status", PassengerConstant.COUPON_STATUS_VALID);
        return ResponseData.buildSuccessResponse(this.passengerCouponMapper.count(params));
    }


    @Override
    public ResponseData<Integer> addBatch(List<PassengerCouponDto> passengerCouponDtoList) {
        return ResponseData.buildSuccessResponse(passengerCouponMapper.addBatch(passengerCouponDtoList));
    }


    @Override
    public ResponseData<List<PassengerCouponExportVo>> listExport(AdminPassengerCouponPageParam couponPageParam) {
        Map<String, Object> map = MapUtils.convertObjToMap(couponPageParam);
        return ResponseData.buildSuccessResponse(passengerCouponMapper.listExport(map));
    }

    @Override
    public ResponseData<PageVo<PassengerCouponDto>> queryOrderPage(AdminPassengerCouponPageParam couponPageParam) {
        PageBounds pageBounds = new PageBounds(couponPageParam.getPageNum(), couponPageParam.getPageSize());
        PageList<PassengerCouponDto> pageList = passengerCouponMapper.queryOrderPage(BeanMap.create(couponPageParam), pageBounds);
        PageVo<PassengerCouponDto> pageVo = new PageVo(couponPageParam.getPageNum(), couponPageParam.getPageSize(),
                pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    @Override
    public ResponseData<Boolean> refundCoupon(String orderUuid) {
        long count = passengerCouponMapper.refundCoupon(orderUuid);
        if (count > 0) {
            return ResponseData.buildSuccessResponse("退回成功", Boolean.TRUE);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "退回失败");
    }

    @Override
    public ResponseData<List<PassengerCouponDto>> findJoinMinCoupon(String passengerUuid, Integer useBusinessType, Double tripTotalFare, String originCity) {
        ApiPasPayCouponParam couponParam = new ApiPasPayCouponParam();
        couponParam.setUserUuid(passengerUuid);
        couponParam.setBusinessType(useBusinessType);
        couponParam.setTotalFare(tripTotalFare);
        couponParam.setOriginCityUuid(originCity);
        return getPayCouponList(couponParam);
    }

    @Override
    public ResponseData<PageVo<PassengerCouponDto>> adminCouponList(AdminPassengerUuidPageParam param) {
        PageBounds pageBounds = new PageBounds(param.getPageNum(), param.getPageSize());
        PageList<PassengerCouponDto> pageList = passengerCouponMapper.queryPage(MapUtils.convertObjToMap(param), pageBounds);
        PageVo<PassengerCouponDto> pageVo = new PageVo(param.getPageNum(), param.getPageSize(),
                pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }
}
